package com.smartsofa.adapter;

import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartsofa.R;
import com.smartsofa.main.AddFamilyMember;
import com.smartsofa.main.FamilyInfo;
import com.smartsofa.util.AsyncImageLoader;
import com.smartsofa.util.CircleImageView;
import com.smartsofa.util.MyUtils;

public class FamilyListAdapter extends BaseExpandableListAdapter {
	
    private LayoutInflater layoutInflater;
	private Context mContext;
    private ArrayList<Map<String, String>> groupArray;
    private ArrayList<ArrayList<Map<String, String>>> childArray;
	protected int familyId;
	private String imageurl;
	private ArrayList<Bitmap> gbitmap;
	private ArrayList<ArrayList<Bitmap>> cbitmap;
	
	private Intent intent;
	private Boolean flag = false;
	private int flagchild = -1;
	private int flaggroup = -1;
	private Boolean isGroup ;
	//异步加载图片
	private AsyncImageLoader imageLoader;

    public FamilyListAdapter(Context context, ArrayList<Map<String, String>> parentData,ArrayList<ArrayList<Map<String, String>>> childData){      
    	this.layoutInflater = LayoutInflater.from(context); 
		this.mContext = context;
		this.groupArray = parentData;
		this.childArray = childData;
		imageLoader = new AsyncImageLoader(context);
    } 
	
	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return childArray.get(arg0).get(arg1);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return arg1;
	}

	@Override
	public View getChildView(int position, int arg1, boolean arg2, View convertView,
			ViewGroup arg4) {
		// TODO Auto-generated method stub
		ImageView user_img;
		TextView member_name;
		isGroup = false;
		Log.i("调用GETCHildVIEW", "1");
		if(convertView==null){  
			convertView = layoutInflater.inflate(R.layout.listview_child, null); 
        }
		user_img=(ImageView) convertView.findViewById(R.id.child_user_img);
		// 预设一个图片
		user_img.setImageResource(R.drawable.user_img2);
 		if (childArray.get(position).get(arg1).containsKey("userImg")&&!("").equals(childArray.get(position).get(arg1).get("userImg"))) {
 			final String imgUrl = (String) childArray.get(position).get(arg1).get("userImg");
 			// 给 ImageView 设置一个 tag
 			user_img.setTag(imgUrl);
 			if (!TextUtils.isEmpty(imgUrl)) {
 				Bitmap bitmap = imageLoader.loadImage(user_img, imgUrl);
 				if (bitmap != null) {
 					user_img.setImageBitmap(bitmap);
 				}
 			}
 		}
		
		member_name=(TextView)convertView.findViewById(R.id.member_name);

		member_name.setText(childArray.get(position).get(arg1).get("userName"));
		return convertView;
	}

	@Override
	public int getChildrenCount(int arg0) {
		// TODO Auto-generated method stub
		return childArray.get(arg0).size();
	}

	@Override
	public Object getGroup(int arg0) {
		// TODO Auto-generated method stub
		return groupArray.get(arg0);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupArray.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getGroupView(final int position, boolean arg1, View convertView, ViewGroup arg3) {
		// TODO Auto-generated method stub
		CircleImageView familyImg;
		TextView familyName;
		TextView memberNum;
		TextView familySign;
		Button familyInfo;
		Button bt_addmember;
		isGroup = true;

		if(convertView == null){
			convertView = layoutInflater.inflate(R.layout.listview_parent, null);
		}
		
		familyImg=(CircleImageView) convertView.findViewById(R.id.parent_family_img);
		familyImg.setImageResource(R.drawable.family_img);
		if (groupArray.get(position).containsKey("familyImg")&&!("").equals(groupArray.get(position).get("familyImg"))) {
 			final String imgUrl = (String) groupArray.get(position).get("familyImg");
 			// 给 ImageView 设置一个 tag
 			familyImg.setTag(imgUrl);
 			if (!TextUtils.isEmpty(imgUrl)) {
 				Bitmap bitmap = imageLoader.loadImage(familyImg, imgUrl);
 				if (bitmap != null) {
 					familyImg.setImageBitmap(bitmap);
 				}
 			}
 		}

		familyName=(TextView)convertView.findViewById(R.id.family_name);
		memberNum=(TextView)convertView.findViewById(R.id.tv_family_membernum);
		familySign=(TextView)convertView.findViewById(R.id.family_signature);
		familyInfo=(Button)convertView.findViewById(R.id.bt_familyinfo);
		bt_addmember=(Button)convertView.findViewById(R.id.bt_addmember);
		
		familyName.setText(groupArray.get(position).get("familyName"));
		memberNum.setText(groupArray.get(position).get("memberNum"));
		familySign.setText(groupArray.get(position).get("signature"));
		
		/**
		 * 判断当前用户是否为家庭组的创建者，如果不是，就不显示邀请家庭组成员的按钮
		 */
		if(MyUtils.getUSERID()!=Integer.valueOf(groupArray.get(position).get("adminId"))){
			bt_addmember.setVisibility(View.INVISIBLE);
		}else{
			bt_addmember.setVisibility(View.VISIBLE);
		}
		
		//家庭组信息
		familyInfo.setOnClickListener(new OnClickListener() {									
			@Override
			public void onClick(View v) {			
				intent=new Intent().setClass(mContext, FamilyInfo.class);
				Bundle bundle=new Bundle();
				bundle.putString("familyId", groupArray.get(position).get("familyId"));
				intent.putExtras(bundle);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
				mContext.startActivity(intent);
			}
		});
		//添加家庭组成员
		bt_addmember.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub				
				intent=new Intent().setClass(mContext, AddFamilyMember.class);
				Bundle bundle=new Bundle();
				bundle.putString("familyId", groupArray.get(position).get("familyId"));
				intent.putExtras(bundle);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
}
